{{extend 'layout.html' }}
<script src="/{{= request.application }}/static/sorttable.js"></script>
<style>
table.sortable thead {
    background-color:#eee;
    color:#666666;
    font-weight: bold;
    cursor: default;
}
</style>
{{ try: }}{{= uplink }}{{ except: }}{{ pass }}
<h1>
{{ if request.function == 'state': }}
  Internal state
{{ else: }}
  {{ if len(request.args) >= 1: }}
    database
    {{ if not request.vars.query: }}table {{= A(request.args[1],_href=URL(r=request, f='select/%s/%s' % tuple(request.args[:2]))) }} select {{ else: }} generic select/update/delete{{ pass }}
    {{ if len(request.args) == 3: }}
      record id {{= A(request.args[2],_href=URL(r=request, f='update/%s/%s/%s' % tuple(request.args[:3]))) }}
    {{ pass }}
  {{ else: }}
    Manage store
  {{ pass }}
{{ pass }}
</h1>


{{ if request.function == 'index': }}
      <h2>{{= A("Setup", _href='/%s/manage/setup'%request.application) }}</h2>
      <i>Here you setup general store information</i>
      <br/><br/>
      <h2>{{= A("Store Categrories", _href=URL(r=request, f='select/store/category')) }}</h2>
      <i>Here you manage the categories of products</i>
      <br/><br/>
      <h2>{{= A("Store Products", _href=URL(r=request, f='select/store/product')) }}</h2>
      <i>Here you manage the actual products you sells</i>
      <br/><br/>
      <h2>{{= A("Store Comments", _href=URL(r=request, f='select/store/comment')) }}</h2>
      <i>Here you manage the comments posted by visitors</i>
      <br/><br/>
      <h2>{{= A("Cleanup Temporary Files", _href=URL(r=request, f='cleanup')) }}</h2>
      <i>This is a maintenance function, run it once a week.</i>
      <br/><br/>
<br/><br/>
{{ pass }}

{{ if request.function == 'select' and len(request.args) > 1: }}
  [ {{= A('insert new %s' % request.args[1], _href=URL(r=request, f='insert/%s/%s' % tuple(request.args[:2]))) }} ]<br/><br/>
    <h2>Rows in table</h2><br/>
{{ elif request.function=='select' and len(request.args) == 1: }}
    <h2>Rows selected</h2><br/>
{{ pass }}
{{ if request.function == 'select' and len(request.args) >= 1: }}
    <form action="{{= URL(r=request,args=request.args[:1]) }}" method="post">
    <table>
    <tr><td width="150px">FILTER CONDITION:</td><td><input type="text" value="{{= query }}" name="query" size="60"/><input type="submit" value="apply"/></td></tr>
    {{ if len(request.args) == 1: }}
    <tr><td>UPDATE STRING:</td><td><input type="text" value="" name="update_string"/> or DELETE ALL: <input type="checkbox" name="delete_all"/> (sure?<select name="delete_all_sure"/><option selected>no</option><option>yes</option></select>)<br/><i>(The SQL FILTER is a condition like "table1.field1='value'". Something like "table1.field1=table2.field2" results in a SQL JOIN. Use AND, OR and (...) to build more complex filters. The UPDATE STRING is an optional expression like "field1='newvalue'". You cannot update or delete the results of a JOIN)</i>

</td></tr>
    {{ pass }}
    </table>
    </form>
    <br/>    <br/>
    {{ if start > 0: }}[ {{= A('previous 100 records',_href=URL(r=request, f='select/%s?query=%s&start=%s' % ('/'.join(request.args), query, start-100))) }} ]{{ pass }}
    {{ if nrecords == 100: }}[ {{= A('next 100 records',_href=URL(r=request, f='select/%s?query=%s&start=%s' % ('/'.join(request.args), query, start+100))) }} ]{{ pass }}
    {{= records }}
     <br/><br/><h2>CSV import/export</h2><br/>
     [ <a href="{{= URL(r=request,f='csv',vars=dict(dbname=request.args[0],query=query)) }}">export as csv file</a> ]
  {{ if len(request.args) == 2: }}
     {{= FORM('or import from csv file ', INPUT(_type='file', _name='csvfile'),INPUT(_type='submit', _value='import')) }}
  {{ pass }}
{{ pass }}

{{ if request.function == 'insert' and len(request.args) > 1: }}  
  <h2>New Record</h2><br/>
  {{= form }}
{{ pass }}

{{ if request.function == 'update' and len(request.args) > 2: }}
  <h2>Edit current record</h2><br/><br/>{{= form }}
{{ pass }}

{{ if request.function == 'state': }}
  <h2>Current request</h2>
  {{= BEAUTIFY(request) }}
  <br/><h2>Current response</h2>
  {{= BEAUTIFY(response) }}
  <br/><h2>Current session</h2>
  {{= BEAUTIFY(session) }}
{{ pass }}